const gulp = require('gulp');

const server = require('gulp-webserver');  //起服务

const url = require('url');

const fs = require('fs');

const path = require('path');


//起服务
gulp.task('server',function(){
    return gulp.src('src')
    .pipe(server({
        port:9090,//端口号
        middleware:function(req,res,next){
            var pathname = url.parse(req.url).pathname;

            if(pathname === '/favicon.ico'){
                res.end('');
                return 
            }

            if(pathname ==='/api/getUid'){ //接口
                setTimeout(() => {
                    res.end(JSON.stringify({code:1,uid:'1234'}))
                },3000)
            }else if(pathname === '/api/getBill'){
                let uid = url.parse(req.url,true).query.uid;
                
                res.end(JSON.stringify({code:1,data:'list'}))
            }else{ //读文件
                pathname = pathname === '/' ? 'index.html' : pathname;
                res.end(fs.readFileSync(path.join(__dirname,'src',pathname)));
            }
        }
    }))
})










